﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DeepEarth.BingMapsToolkit.Common.Entities;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;

namespace SMIL.DE.Extensions
{
    public delegate void GetWMSCapabilitiesEventHandler(object sender, GetWMSCapabilitiesEventArgs args);

    public class GetWMSCapabilitiesEventArgs : EventArgs
    {
        public System.Collections.Generic.List<Layer> SubLayers { get; set; }
        //public SMILWMSLayerDefinition smilWMSLayerDefinition { get; set; }
    }

    public class SMILWMSLayerDefinition : LayerDefinition
    {
        public string uri;
        public string WMSLayerName;
        public int sortorder;

        public event GetWMSCapabilitiesEventHandler GetCompleted;

        protected void OnGetCompleted(System.Collections.Generic.List<Layer> subLayers)
        {
            if (GetCompleted != null)
                GetCompleted(this, new GetWMSCapabilitiesEventArgs { SubLayers = subLayers});
        }

        //public string[] subLayers;
        //Constructor Called by XAML instantiation;
        public void GetCapabilities()
        {
            string strURI = uri;
            if (strURI.EndsWith("/export", StringComparison.OrdinalIgnoreCase))
                strURI = strURI.Substring(0, strURI.LastIndexOf("/"));
            else
                return; // This is not so very good logic. It would be better to define it in the configuration. johan 2010-11-08

            strURI += "?f=json";

            System.Net.WebClient client = new System.Net.WebClient();
            client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri(strURI));

        }

        void client_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            System.Collections.Generic.List<Layer> subLayers = new System.Collections.Generic.List<Layer>();

            RestDescription restDesc = new RestDescription();

            DataContractJsonSerializer contract = new DataContractJsonSerializer(restDesc.GetType());
            MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));
            restDesc = (RestDescription)contract.ReadObject(stream);
            stream.Close();

            foreach(Layer layer in restDesc.layers)
                subLayers.Add(layer);
            OnGetCompleted(subLayers);
        }
    }
    public class RestDescription
    {
        //{
        //    "serviceDescription":"Administrative data från SkogsGIS geodatabas",
        //    "mapName":"Lager",
        //    "description":"",
        //    "copyrightText":"",
        //    "layers":[
        //        {"id":0,"name":"Fastighetskarta","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null},
        //        {"id":1,"name":"Markägande bolag","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null},
        //        {"id":2,"name":"Förvaltning och distrikt","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null},
        //        {"id":3,"name":"L_K_F","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null}],
        //    "spatialReference":{"wkid":102113},
        //    "singleFusedMapCache":false,
        //    "initialExtent":{"xmin":1924429.4975401,"ymin":9051326.09842048,"xmax":1932030.46997674,"ymax":9055122.87684737,"spatialReference":{"wkid":102113}},
        //    "fullExtent":{"xmin":1142191.70966315,"ymin":8283134.19917557,"xmax":2907985.26664727,"ymax":10909832.0595507,"spatialReference":{"wkid":102113}},
        //    "units":"esriMeters",
        //    "supportedImageFormatTypes":"PNG32,PNG24,PNG,JPG,DIB,TIFF,EMF,PS,PDF,GIF,SVG,SVGZ",
        //    "documentInfo":{"Title":"Administrative data","Author":"leo","Comments":"","Subject":"Karta för VindGIS","Category":"","Keywords":"","AntialiasingMode":"None","TextAntialiasingMode":"Normal"}
        //}
        public string serviceDescription {get; set;}
        public string mapName { get; set; }
        public string description { get; set; }
        public string copyrightText { get; set; }
        public System.Collections.Generic.List<Layer> layers { get; set; }
        public SpatialReference spatialReference { get; set; }
        public string singleFusedMapCache { get; set; }
        public Extent initialExtent { get; set; }
        public Extent fullExtent { get; set; }
        public string units { get; set; }
        public string supportedImageFormatTypes { get; set; }
        public DocumentInfo documentInfo { get; set; }
    }
    public class DocumentInfo
    {
        //    "documentInfo":{"Title":"Administrative data","Author":"leo","Comments":"","Subject":"Karta för VindGIS","Category":"","Keywords":"","AntialiasingMode":"None","TextAntialiasingMode":"Normal"}
        public string Title { get; set; }
        public string Author { get; set; }
        public string Comments { get; set; }
        public string Subject { get; set; }
        public string Category { get; set; }
        public string Keywords { get; set; }
        public string AntialiasingMode { get; set; }
        public string TextAntialiasingMode { get; set; }
    }
    public class Extent
    {
        //    "initialExtent":{"xmin":1924429.4975401,"ymin":9051326.09842048,"xmax":1932030.46997674,"ymax":9055122.87684737,"spatialReference":{"wkid":102113}},
        public string xmin { get; set; }
        public string ymin { get; set; }
        public string xmax { get; set; }
        public string ymax { get; set; }
        public SpatialReference spatialReference { get; set; }
    }
    public class Layer
    {
        //    "layers":[
        //        {"id":0,"name":"Fastighetskarta","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null},
        //        {"id":1,"name":"Markägande bolag","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null},
        //        {"id":2,"name":"Förvaltning och distrikt","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null},
        //        {"id":3,"name":"L_K_F","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null}],
        public string id { get; set; }
        public string name { get; set; }
        public string parentLayerId { get; set; }
        public string defaultVisibility { get; set; }
        public string subLayerIds { get; set; }
    }
    public class SpatialReference
    {
        //    "spatialReference":{"wkid":102113},
        public string wkid { get; set; }
    }
}
